package com.it.you.common.utils;

import com.google.common.base.Joiner;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Conv {

    private static final SimpleDateFormat FORMAT_DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat FORMAT_TIME = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");

    public Conv() {
    }

    public static int asInteger(Object o) {
        return asInteger(o, 0);
    }

    public static int asInteger(Object o, int df) {
        if (o == null) {
            return df;
        } else if (o instanceof Integer) {
            return (Integer)o;
        } else if (o instanceof Byte) {
            return (Byte)o;
        } else if (o instanceof Short) {
            return (Short)o;
        } else if (o instanceof Double) {
            return ((Double)o).intValue();
        } else if (o instanceof Float) {
            return ((Float)o).intValue();
        } else if (o instanceof BigInteger) {
            return ((BigInteger)o).intValue();
        } else if (o instanceof BigDecimal) {
            return ((BigDecimal)o).intValue();
        } else {
            try {
                return Integer.parseInt(o.toString());
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static long asLong(Object o) {
        return asLong(o, 0L);
    }

    public static long asLong(Object o, long df) {
        if (o == null) {
            return df;
        } else if (o instanceof Long) {
            return (Long)o;
        } else if (o instanceof Integer) {
            return (long)(Integer)o;
        } else if (o instanceof Byte) {
            return (long)(Byte)o;
        } else if (o instanceof Short) {
            return (long)(Short)o;
        } else if (o instanceof Double) {
            return ((Double)o).longValue();
        } else if (o instanceof Float) {
            return ((Float)o).longValue();
        } else if (o instanceof BigInteger) {
            return ((BigInteger)o).longValue();
        } else if (o instanceof BigDecimal) {
            return ((BigDecimal)o).longValue();
        } else {
            try {
                return Long.parseLong(o.toString());
            } catch (Exception var4) {
                return df;
            }
        }
    }

    public static String asString(Object o) {
        return asString(o, "");
    }

    public static String asString(Object o, String df) {
        try {
            return o.toString();
        } catch (Exception var3) {
            return df;
        }
    }

    public static short asShort(Object o) {
        return asShort(o, (short)0);
    }

    public static short asShort(Object o, short df) {
        if (o == null) {
            return df;
        } else if (o instanceof Byte) {
            return (short)(Byte)o;
        } else if (o instanceof Short) {
            return (Short)o;
        } else if (o instanceof BigInteger) {
            return ((BigInteger)o).shortValue();
        } else if (o instanceof BigDecimal) {
            return ((BigDecimal)o).shortValue();
        } else {
            try {
                return Short.parseShort(o.toString());
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static boolean asBoolean(Object o) {
        return asBoolean(o, false);
    }

    public static boolean asBoolean(Object o, boolean df) {
        if (o == null) {
            return df;
        } else if (o instanceof Boolean) {
            return (Boolean)o;
        } else if (!(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer)) {
            if (!(o instanceof String)) {
                try {
                    return Boolean.parseBoolean(o.toString());
                } catch (Exception var3) {
                    return df;
                }
            } else {
                String v = asString(o);
                return v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes");
            }
        } else {
            return asInteger(o, 0) != 0;
        }
    }

    public static float asFloat(Object o) {
        return asFloat(o, 0.0F);
    }

    public static float asFloat(Object o, float df) {
        if (o == null) {
            return df;
        } else if (o instanceof Float) {
            return (Float)o;
        } else if (!(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer)) {
            try {
                return Float.parseFloat(o.toString());
            } catch (Exception var3) {
                return df;
            }
        } else {
            return (float)asLong(o, 0L);
        }
    }

    public static double asDouble(Object o) {
        return asDouble(o, 0.0D);
    }

    public static double asDouble(Object o, double df) {
        if (o == null) {
            return df;
        } else if (o instanceof Double) {
            return (Double)o;
        } else if (o instanceof Float) {
            return (double)(Float)o;
        } else if (!(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer)) {
            if (o instanceof BigDecimal) {
                return ((BigDecimal)o).doubleValue();
            } else {
                try {
                    return Double.parseDouble(o.toString());
                } catch (Exception var4) {
                    return df;
                }
            }
        } else {
            return (double)asLong(o, 0L);
        }
    }

    public static BigDecimal asDecimal(Object o) {
        return asDecimal(o, BigDecimal.ZERO);
    }

    public static BigDecimal asDecimal(Object o, BigDecimal df) {
        if (o == null) {
            return df;
        } else if (o instanceof BigDecimal) {
            return (BigDecimal)o;
        } else if (!(o instanceof Long) && !(o instanceof Integer) && !(o instanceof Byte) && !(o instanceof Short)) {
            if (!(o instanceof Float) && !(o instanceof Double)) {
                try {
                    return BigDecimal.valueOf(asDouble(o));
                } catch (Exception var3) {
                    return df;
                }
            } else {
                return BigDecimal.valueOf(asDouble(o));
            }
        } else {
            return BigDecimal.valueOf(asLong(o));
        }
    }

    public static Date asDate(Object o) {
        return asDate(o, new Date());
    }

    public static Date asDate(Object o, Date df) {
        if (o == null) {
            return df;
        } else if (o instanceof Date) {
            return (Date)o;
        } else if (o instanceof Long) {
            return new Date((Long)o);
        } else {
            if (o instanceof Integer || o instanceof String && ((String)o).length() == 8) {
                String v = o.toString();
                if (v.length() == 8) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Integer.parseInt(v.substring(0, 4)), Integer.parseInt(v.substring(5, 6)) - 1, Integer.parseInt(v.substring(7, 8)));
                    return calendar.getTime();
                }
            }

            try {
                return (new DateTime(o.toString())).toDate();
            } catch (Exception var4) {
                return df;
            }
        }
    }

    public static String DateTimeStr(Date date) {
        return FORMAT_DATE_TIME.format(date);
    }

    public static String TimeStr(Date date) {
        return FORMAT_TIME.format(date);
    }

    public static String DateStr(Date date) {
        return FORMAT_DATE.format(date);
    }

    public static void main(String[] args) {
        Object o1 = System.currentTimeMillis();
        Object o2 = "20160223";
        Object o3 = "2016/02/23T08:09:01";
        Object o4 = "2016-02-23 08:09:01";
        System.out.println(DateTimeStr(asDate(o1)));
        System.out.println(DateTimeStr(asDate(o2)));
        System.out.println(DateTimeStr(asDate(o3)));
        System.out.println(DateTimeStr(asDate(o4)));
    }

    public static String NS(Object o) {
        return NS(o, "");
    }

    public static String NS(Object o, String df) {
        try {
            return o instanceof String[] ? Joiner.on(",").join((String[])o) : o.toString();
        } catch (Exception var3) {
            return df;
        }
    }

    public static Double NDBorNull(Object o, Double df) {
        if (o == null) {
            return df;
        } else if (o instanceof Double) {
            return (Double)o;
        } else {
            try {
                return o instanceof Float ? ((Float)o).doubleValue() : !(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer) ? Double.parseDouble(o.toString()) : NIorNull(o, (Integer)null).doubleValue();
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static BigDecimal NDec(Object o) {
        return NDec(o, BigDecimal.ZERO);
    }

    public static Integer NIorNull(Object o, Integer df) {
        if (o == null) {
            return df;
        } else if (o instanceof Integer) {
            return (Integer)o;
        } else {
            try {
                if (o instanceof Byte) {
                    return ((Byte)o).intValue();
                } else if (o instanceof Short) {
                    return ((Short)o).intValue();
                } else if (o instanceof BigInteger) {
                    return ((BigInteger)o).intValue();
                } else {
                    return o instanceof Double ? ((Double)o).intValue() : o instanceof BigDecimal ? ((BigDecimal)o).intValue() : Integer.parseInt(o.toString());
                }
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static Long NLorNull(Object o, Long df) {
        if (o == null) {
            return df;
        } else if (o instanceof Long) {
            return (Long)o;
        } else {
            try {
                if (o instanceof Integer) {
                    return ((Integer)o).longValue();
                } else if (o instanceof Byte) {
                    return ((Byte)o).longValue();
                } else if (o instanceof Short) {
                    return ((Short)o).longValue();
                } else {
                    return o instanceof BigInteger ? ((BigInteger)o).longValue() : o instanceof BigDecimal ? ((BigDecimal)o).longValue() : Long.parseLong(o.toString());
                }
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static BigDecimal NDec(Object o, BigDecimal df) {
        if (o == null) {
            return df;
        } else if (o instanceof BigDecimal) {
            return (BigDecimal)o;
        } else {
            try {
                if (!(o instanceof Long) && !(o instanceof Integer) && !(o instanceof Byte) && !(o instanceof Short)) {
                    return !(o instanceof Float) && !(o instanceof Double) ? BigDecimal.valueOf(NDBorNull(o, (Double)null)) : BigDecimal.valueOf(NDBorNull(o, (Double)null));
                } else {
                    return BigDecimal.valueOf(NLorNull(o, (Long)null));
                }
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static int NI(Object o) {
        return NI(o, 0);
    }

    public static int NI(Object o, int df) {
        if (o == null) {
            return df;
        } else if (o instanceof Integer) {
            return (Integer)o;
        } else if (o instanceof Byte) {
            return (Byte)o;
        } else if (o instanceof Short) {
            return (Short)o;
        } else if (o instanceof BigInteger) {
            return ((BigInteger)o).intValue();
        } else if (o instanceof Double) {
            return ((Double)o).intValue();
        } else if (o instanceof BigDecimal) {
            return ((BigDecimal)o).intValue();
        } else {
            try {
                return o.toString().contains(".") ? (new BigDecimal(o.toString())).intValue() : Integer.parseInt(o.toString());
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static double NDB(Object o) {
        return NDB(o, 0.0D);
    }

    public static double NDB(Object o, double df) {
        if (o == null) {
            return df;
        } else if (o instanceof Double) {
            return (Double)o;
        } else if (o instanceof Float) {
            return (double)(Float)o;
        } else if (!(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer)) {
            try {
                return Double.parseDouble(o.toString());
            } catch (Exception var4) {
                return df;
            }
        } else {
            return (double)NI(o, 0);
        }
    }

    public static boolean NB(Object o) {
        return NB(o, false);
    }

    public static boolean NB(Object o, boolean df) {
        if (o == null) {
            return df;
        } else if (o instanceof Boolean) {
            return (Boolean)o;
        } else if (!(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer)) {
            if (!(o instanceof String)) {
                try {
                    return Boolean.parseBoolean(o.toString());
                } catch (Exception var3) {
                    return df;
                }
            } else {
                String v = NS(o);
                return v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes");
            }
        } else {
            return NI(o, 0) != 0;
        }
    }

    public static float NFloat(Object o) {
        return NFloat(o, 0.0F);
    }

    public static float NFloat(Object o, float df) {
        if (o == null) {
            return df;
        } else if (o instanceof Float) {
            return (Float)o;
        } else if (!(o instanceof Short) && !(o instanceof Long) && !(o instanceof Byte) && !(o instanceof Integer)) {
            try {
                return Float.parseFloat(o.toString());
            } catch (Exception var3) {
                return df;
            }
        } else {
            return (float)NI(o, 0);
        }
    }

    public static long NL(Object o) {
        return NL(o, 0L);
    }

    public static long NL(Object o, long df) {
        if (o == null) {
            return df;
        } else if (o instanceof Long) {
            return (Long)o;
        } else if (o instanceof Integer) {
            return (long)(Integer)o;
        } else if (o instanceof Byte) {
            return (long)(Byte)o;
        } else if (o instanceof Short) {
            return (long)(Short)o;
        } else if (o instanceof BigInteger) {
            return ((BigInteger)o).longValue();
        } else if (o instanceof BigDecimal) {
            return ((BigDecimal)o).longValue();
        } else {
            try {
                return o.toString().contains(".") ? (new BigDecimal(o.toString())).longValue() : Long.parseLong(o.toString());
            } catch (Exception var4) {
                return df;
            }
        }
    }

    public static short NShort(Object o) {
        return NShort(o, (short)0);
    }

    public static short NShort(Object o, short df) {
        if (o == null) {
            return df;
        } else if (o instanceof Byte) {
            return (short)(Byte)o;
        } else if (o instanceof Short) {
            return (Short)o;
        } else if (o instanceof BigInteger) {
            return ((BigInteger)o).shortValue();
        } else if (o instanceof BigDecimal) {
            return ((BigDecimal)o).shortValue();
        } else {
            try {
                return Short.parseShort(o.toString());
            } catch (Exception var3) {
                return df;
            }
        }
    }

    public static Date NDT(Object o) {
        return NDT(o, new Date());
    }

    public static Date NDT(Object o, Date df) {
        if (o == null) {
            return df;
        } else if (o instanceof Date) {
            return (Date)o;
        } else if (o instanceof Long) {
            return new Date((Long)o);
        } else {
            if (o instanceof Integer) {
                String v = o.toString();
                if (v.length() == 8) {
                    return new Date(Integer.parseInt(v.substring(0, 4)), Integer.parseInt(v.substring(5, 6)), Integer.parseInt(v.substring(7, 8)));
                }
            }

            try {
                return DateUtils.parseDate(o.toString(), new String[0]);
            } catch (Exception var3) {
                return df;
            }
        }
    }
}
